//
//  FPRankingViewController.m
//  BSLCar
//
//  Created by 梁毅 on 15/5/23.
//  Copyright (c) 2015年 lyz. All rights reserved.
//

#import "FPRankingViewController.h"
#import "OnMarkDetailViewController.h"

@interface FPRankingViewController ()

{
    FirstPageRankingRequest *request;
    UIImageView *accessoryImageView;
}

@end

@implementation FPRankingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
    item.title = @"返回";
    self.navigationItem.backBarButtonItem = item;
    [item release];
    
    accessoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    
    self.navigationItem.title = @"销售排行";
    
    
    
    NSArray *array = [NSArray arrayWithObjects:@"热门",@"豪华",@"suv",@"其他", nil];
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
    segment.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
    segment.selectedSegmentIndex = 0;
    [segment addTarget:self action:@selector(tiaozhuan:) forControlEvents:UIControlEventValueChanged];
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height- 110) style:UITableViewStylePlain];
//    tableview.separatorColor = [UIColor clearColor];
//    self.dataArray = [NSMutableArray array];
    request = [[FirstPageRankingRequest alloc]init];
    self.dataArray = [request getParserResult:@"1"];
    
    tableview.delegate = self;
    tableview.dataSource = self;
    [self.view addSubview:tableview];
    
    [self.view addSubview:segment];
    [segment release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    FirstPageRankingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[FirstPageRankingTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }

    NSArray *array = [self.dataArray objectAtIndex:indexPath.section];
    
    FPRankingModel *model = [array objectAtIndex:indexPath.row + 1];
    cell.priceLabel.text = [NSString stringWithFormat:@"%@万", model.price];
    cell.labelName1.text = model.name1;
    NSString *str = model.imgurl;
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    [cell.mimageview sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Bei"]];
    cell.countLabel.text = [NSString stringWithFormat:@"近一个月订单数: %@", model.ordercount];
    if (indexPath.row < 3) {
        [cell.button setImage:[UIImage imageNamed:@"trophy_b@2x.png"] forState:UIControlStateNormal];
    } else {
        [cell.button setImage:[UIImage imageNamed:@"trophy@2x.png"] forState:UIControlStateNormal];
    }
    [cell.button setTitle:[NSString stringWithFormat:@"%ld", indexPath.row + 1] forState:UIControlStateNormal];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = [self.dataArray objectAtIndex:section];
    return array.count - 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //33
    return self.dataArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OnMarkDetailViewController *onMark = [[OnMarkDetailViewController alloc] init];
    NSArray *array = [self.dataArray objectAtIndex:indexPath.section];
    FPRankingModel *model = [array objectAtIndex:indexPath.row + 1];
    onMark.idDetail = [NSString stringWithFormat:@"%ld", model.id1];
    onMark.carName = model.name1;
    [self.navigationController pushViewController:onMark animated:YES];
    [onMark release];
}

//分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSArray *array = [self.dataArray objectAtIndex:section];
    return [array objectAtIndex:0];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 95;
}
- (void)tiaozhuan:(id)sender
{
    self.dataArray = [request getParserResult:[NSString stringWithFormat:@"%ld",(long)[sender selectedSegmentIndex] + 1]];
    [tableview reloadData];
    
//     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
//
//    [tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    
//    [tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
